<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Caidan extends CF_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->model('caidan_mdl');
		$this->uid = $this->user_login->is_logged_in() ? $this->user_login->get_user_id() : 0;
	}

	public function index()
	{
		$this->lists();
	}
	/**
	 * 菜谱内容页
	 * @param  string $id [description]
	 * @return [type]     [description]
	 */
	public function views($id='', $page = '')
	{
		$page = max(1, intval($page));
		$limit = 12; //每页显示数
		$offset = ($page - 1) * $limit;
		$data_count = 500;

		$this->load->library('pagination');
		$config['base_url'] = site_url('caidan/views/'.$id.'/');
		$config['total_rows'] = $data_count;
		$config['per_page'] = $limit; 
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config); 

		$this->load->model('user/users','users');
		$this->load->model('user/user_favorite','ufavorite');
		$this->load->model('user/user_guanzhu','uguanzhu');
		$this->load->helper('text');
		
		/** 如为空，则跳转 */
		if (empty($id) || !is_numeric($id)) redirect(site_url());

		//获取菜单信息
		$data['caidan_info'] = $this->caidan_mdl->get_by(array('id'=>$id));

		/** 不存在? */
		if (!$data['caidan_info']) show_404();

		$this->caidan_mdl->update_views($id);	//更新菜谱浏览次数
		//获取发布此菜的用户
		$query_u = $this->users->get_user_by_id($data['caidan_info']['uid'])->row();
		$data['user_info'] = $query_u;

		$data['if_guanzhu'] = $this->uguanzhu->get_user_guanzhu($data['caidan_info']['uid'], $this->uid); //guanzhu

		$data['if_caidan_fav'] = $this->ufavorite->get_caidan_favorite($id, $this->uid); //是否收藏

		$data['caipu_user_avartar'] = $this->user_login->get_avatar_dir($data['caidan_info']['uid']);


		$caipu_array = $this->caidan_mdl->get_caipu_by_caidan($id, $offset, $limit)->result_array();

		$rows_array = array();
		foreach ($caipu_array as $value) 
		{
			$value['coverImage'] = server_img($value['coverImage']);
			$rows_array[] = $value;
		}
		$data['caipu_list'] = $rows_array;
		//谁收藏了这个菜单
		//$data['caidan_fav_user'] = $this->ufavorite->get_caidan_fav_user($id, 12)->result_array();

		$this->load->view('caidan_views',$data);
	}

	public function lists($page = '')
	{
		$page = max(1, intval($page));
		$limit = 20; //每页显示数
		$offset = ($page - 1) * $limit;
		$caidan_list = $this->caidan_mdl->get(array('status'=>1), $offset, $limit)->result_array();
		$data_count = 50000; 

		
		/* 分页配置 */
		$this->load->library('pagination');
		$config['base_url'] = site_url('caidan/lists/');
		$config['total_rows'] = $data_count;
		$config['per_page'] = $limit; 

		$this->pagination->initialize($config); 

		$data['caidan_list'] = $caidan_list;

		$this->load->view('caidan',$data);
	}

	/**
	 * 排行榜
	 * @return [type] [description]
	 */
	public function ranking()
	{
	}

	/**
	 * 菜单
	 * @return [type] [description]
	 */
	public function caidan()
	{
	}

}

/* End of file caipu.php */
/* Location: ./application/controllers/caipu.php */